package com.example.foolishbirddemo;

import android.accounts.NetworkErrorException;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;

import com.foolishbird.sdk.api.FBSDK;
import com.foolishbird.sdk.api.WanSDKCallback;
import com.foolishbird.sdk.params.bean.Goods;
import com.foolishbird.sdk.params.bean.RoleInfo;
import com.foolishbird.sdk.params.bean.RoleStatus;
import com.foolishbird.sdk.utils.WL;

import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentActivity;

import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.Calendar;

public class MainActivity extends FragmentActivity {

    private TextView version;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        hideVirtualKeysAndFullScreen();
        WL.writeLogs(true);
        setContentView(getResources().getIdentifier("activity_main", "layout", getPackageName()));

        version = (TextView) findViewById(getResources().getIdentifier("version_text", "id", getPackageName()));
        version.setText(getVersionName());

        login = findViewById(getResources().getIdentifier("login_test", "id", getPackageName()));
        FBSDK.getInstance().onCreate(this);
    }


    String getVersionName() {
        try {
            PackageManager pm = getPackageManager();
            PackageInfo info = null;
            info = pm.getPackageInfo(getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    public void openFloatWindow(View v) {
        FBSDK.getInstance().openWindow();
    }

    public void closeFloatWindow(View v) {
        FBSDK.getInstance().closeWindow();
    }

    @Override
    public void onBackPressed() {
//        FBSDK.getInstance().onBackPressed();
        //super.onBackPressed();
        FBSDK.getInstance().exitApp(this);
    }

    public void onLoginTest(View v) {

        if (!FBSDK.getInstance().isLoggedIn()) {
            FBSDK.getInstance().login(new WanSDKCallback() {
                @Override
                public void onWanSDKResponse(int stateCode, String message, String jsonObject) {
                    if (1 == stateCode || 4 == stateCode) {
                        //登录成功 以json形式返回用户信息uid，ticket，expiresin
                        //示例：{"uid":"1061758","ticket":"5ce0c0a17838375fba109d44d202f97b","expiresIn":7200}

                    } else if (2 == stateCode) {
                        //取消登录操作

                    }else if (5 ==stateCode){
                      //登出操作
                    }else {
                        //登录失败
                    }
//                    uploadRoleInfo(null);
                }

                @Override
                public void onWanSDKException(Exception ex) {
                    //登录失败
                }
            });
        } else {
            FBSDK.getInstance().logout();
            login.setText("登录（测试）");
        }
    }

    public void uploadRoleInfo(View v) {
        RoleInfo roleinfo = new RoleInfo();
        roleinfo.setRoleLevel("11");
        roleinfo.setServerId("1085");
        roleinfo.setServerName("995测试区");
        roleinfo.setRoleName("杭高岑");
        roleinfo.setRoleId("65542");
        FBSDK.getInstance().uploadRoleInfo(roleinfo, RoleStatus.UPGRADE, new WanSDKCallback() {
            @Override
            public void onWanSDKResponse(int stateCode, String message, String jsonObject) {

            }

            @Override
            public void onWanSDKException(Exception ex) {

            }
        });
        FBSDK.getInstance().startPushWindowMessageTask(roleinfo);
    }

//    public void queryPreventAddictionInfo(View v) {
//        FBSDK.getInstance().queryPreventAddictionInfo(new Callback());
//    }

    public void onMobliePayTest(View v) {
        final EditText et = new EditText(MainActivity.this);
        et.setHint("输入充值金额(单位分)");
        et.setInputType(EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
        new AlertDialog.Builder(MainActivity.this).setTitle("设置充值金额(分)").setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String input = et.getText().toString().trim();
                        if (TextUtils.isEmpty(input)) {
                            Toast.makeText(getApplicationContext(), "不能为空", Toast.LENGTH_SHORT).show();
                        } else{
                            try {
                                int MONEY = Integer.valueOf(input);
                                if (MONEY < 1) {
                                    Toast.makeText(getApplicationContext(), "请输入金额大于0", Toast.LENGTH_SHORT).show();
                                } else {
                                    callPay(MONEY);
                                }
                            } catch (Exception e) {
                                Toast.makeText(getApplicationContext(), "请输入正确的格式", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }).show();
//        Goods goods = new Goods("60灵玉", 100);
//        goods.setGoodsId("11");
//        goods.setGoodsDesc("购买60灵玉");
//        goods.setServerId(100);
//        goods.setServerName("双线二服");
//        goods.setRoleId("5456");
//        goods.setRoleName("测试角色");
//        goods.setGainCount(1);
//        goods.setCpOrderId(Calendar.getInstance().getTimeInMillis()+"");
//        goods.setExtraData("A510531805265734");
//        FBSDK.getInstance().pay(goods, new WanSDKCallback() {
//            @Override
//            public void onWanSDKResponse(int stateCode, String message, String jsonObject) {
//                if (1 == stateCode) {
//                    //支付成功
//                } else if (2 == stateCode) {
//                    //支付取消
//                } else if (4 == stateCode) {
//                    //未登录
//
//                } else if (5 == stateCode) {
//                    //未知错误
//                } else {
//                    //支付失败
//                }
//            }
//
//            @Override
//            public void onWanSDKException(Exception ex) {
//
//            }
//        });
    }

    private void callPay(int money){
                Goods goods = new Goods(money+"灵玉", money);
        goods.setGoodsId("11");
        goods.setGoodsDesc("购买60灵玉");
        goods.setServerId(100);
        goods.setServerName("双线二服");
        goods.setRoleId("5456");
        goods.setRoleName("测试角色");
        goods.setGainCount(1);
        goods.setCpOrderId(Calendar.getInstance().getTimeInMillis()+"");
        goods.setExtraData("A510531805265734");
        FBSDK.getInstance().pay(goods, new WanSDKCallback() {
            @Override
            public void onWanSDKResponse(int stateCode, String message, String jsonObject) {
                if (1 == stateCode) {
                    //支付成功
                } else if (2 == stateCode) {
                    //支付取消
                } else if (4 == stateCode) {
                    //未登录

                } else if (5 == stateCode) {
                    //未知错误
                } else {
                    //支付失败
                }
            }

            @Override
            public void onWanSDKException(Exception ex) {

            }
        });
    }

    public void deleteDownloadApkFile(View view) {
//        File file = VersionUpdateDownloader.getImpl().getDownloadApkVersionFile();
//        if (file != null && file.delete()) {
//            Toast.makeText(this, "删除下载文件成功！", Toast.LENGTH_SHORT).show();
//        } else {
//            Toast.makeText(this, "删除下载文件失败！", Toast.LENGTH_SHORT).show();
//        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        FBSDK.getInstance().onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        FBSDK.getInstance().onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        FBSDK.getInstance().onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        FBSDK.getInstance().onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        FBSDK.getInstance().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FBSDK.getInstance().onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FBSDK.getInstance().onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        FBSDK.getInstance().onConfigurationChanged(newConfig);
    }

    protected void hideVirtualKeysAndFullScreen() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

//    private class Callback implements WanSDKCallback {
//
//        @Override
//        public void onWanSDKResponse(int stateCode, String message, String jsonObject) {
//            StringBuilder builder = new StringBuilder();
//            builder.append("code: ").append(stateCode).append(" ,message: ").append(message);
//            if (jsonObject != null) {
//                builder.append(" ,data: ").append(jsonObject);
//                if (!TextUtils.isEmpty(jsonObject)) {
//                    login.setText("登出（测试）");
//                }
//            }
//            Toast.makeText(MainActivity.this, builder.toString(), Toast.LENGTH_LONG).show();
//            WL.d("TAG", "jsonObject: " + builder.toString());
//        }
//
//        @Override
//        public void onWanSDKException(Exception ex) {
//            if (ex instanceof NetworkErrorException) {
//                Toast.makeText(MainActivity.this, "NetworkErrorException", Toast.LENGTH_LONG).show();
//            } else if (ex instanceof IOException) {
//                Toast.makeText(MainActivity.this, "IOException", Toast.LENGTH_LONG).show();
//            } else {
//                Toast.makeText(MainActivity.this, "OtherException", Toast.LENGTH_LONG).show();
//            }
//        }
//    }
}